
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Queue;

public class GenericQueueDAO extends AutoKeys implements
		org.jetnuke.dao.QueueDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.QueueModel#retrieveByPK(int)
	 */
	public Queue retrieveByPK(int id)throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("QueueModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		Queue  s = new Queue();
		
		if (rs.next()) {
			fillQueueFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillQueueFromResultSet(Queue s, ResultSet rs)throws Exception {
		s.setALANGUAGE(rs.getString("ALANGUAGE"));
		s.setQID(rs.getInt("QID"));
		s.setSTORY(rs.getString("STORY"));
		s.setSTORYEXT(rs.getString("STORYEXT"));
		s.setSUBJECT(rs.getString("SUBJECT"));
		s.setTIMESTAMP(rs.getTimestamp("TIMESTAMP"));
		s.setTOPIC(rs.getInt("TOPIC"));
		s.setUID(rs.getInt("UID"));
		s.setUNAME(rs.getString("UNAME"));
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.QueueModel#selectAllOrderTimestamp()
	 */
	public List selectAllOrderTimestamp() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("QueueModel.selectAllOrderTimestamp"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Queue u = new Queue();
			fillQueueFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.QueueModel#deleteByID(int)
	 */
	public void deleteByID(int qid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("QueueModel.deleteByID"));
		p.setInt(1,qid);
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.QueueModel#countAll()
	 */
	public int countAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("QueueModel.countAll"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;

	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.QueueModel#addNew(org.jetnuke.entities.Queue)
	 */
	public int addNew(Queue queue) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("QueueModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("QueueModel.lastGeneratedQueueID"));
		initNewQueue(queue, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;	
	}

	/**
	 * @param queue
	 * @param p
	 */
	private void initNewQueue(Queue queue, PreparedStatement p) throws Exception{
		p.setString(1,queue.getALANGUAGE());
		p.setString(2,queue.getSTORY());
		p.setString(3,queue.getSTORYEXT());
		p.setString(4,queue.getSUBJECT());
		p.setTimestamp(5,new Timestamp(queue.getTIMESTAMP().getTime()));
		p.setInt(6,queue.getTOPIC());
		p.setInt(7,queue.getUID());
		p.setString(8,queue.getUNAME());
		
	}

}
